package com.example.contact;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{
	
	private static final int VERSION = 1;
	private SQLiteDatabase db;

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	public DatabaseHelper(Context context, String name) {
		this(context, name, VERSION);
	}

	public DatabaseHelper(Context context, String name, int version) {
		this(context, name, null, VERSION);
	}

	public void onCreate(SQLiteDatabase db) {
	
		StringBuffer tableCreate = new StringBuffer();
		tableCreate.append("create table user (_id interger primar key autoincrement,")
					.append("name text,")
					.append("imageid int")
					.append("mobilePhone text,")
					.append("familyPhone text,")
					.append("officePhone text,")
					.append("address text,")
					.append("position text,")
					.append("company text,")
					.append("email text,")
					.append("othercontact text,")
					.append("zipCode text,")
					.append("remark text,)");
		db.execSQL(tableCreate.toString());
	}

	private void openDataBase() {
		if(db == null) {
			db = this.getWritableDatabase();
		}
	}
	
	
	public void save(User user) {
		
		openDataBase();
		ContentValues value = new ContentValues();
		value.put("name", user.name);
		value.put("mobilphone", user.mobilePhone);
		value.put("familyphone", user.familyPhone);
		value.put("officephone", user.officePhone);
		value.put("position", user.positon);
		value.put("company", user.company);
		value.put("address", user.address);
		value.put("email", user.email);
		value.put("othercontact", user.otherContact);
		value.put("zipCode", user.zipCode);
		value.put("imageid", user.imageId);
		value.put("remark", user.remark);
		
		db.insert("user", null, value);
		
	}
	
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "drop table if exists user";
		db.execSQL(sql);
		onCreate(db);
	}

}
